﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SchemaProvider;

namespace Kenly.DBFramework.ORMCodeHelper
{
    public partial class frmQueryHelper : Form
    {
        DataTable _selectedProperties;
        ICodeProvider _codeProvider;

        public DataTable SelectedProperties
        {
            get
            {
                return _selectedProperties;
            }
            set
            {
                _selectedProperties = value;
            }
        }

        public ICodeProvider CodeProvider
        {
            get
            {
                return _codeProvider;
            }
            set
            {
                _codeProvider = value;
            }
        }

        public frmQueryHelper()
        {
            InitializeComponent();
        }

        private void frmQueryHelper_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void lstProperties_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowCode();
        }

        private void cbReturnTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowCode();
        }

        private void txtMethodName_TextChanged(object sender, EventArgs e)
        {
            ShowCode();
        }

        private void rbtnAnd_CheckedChanged(object sender, EventArgs e)
        {
            ShowCode();
        }

        private void rbtnOr_CheckedChanged(object sender, EventArgs e)
        {
            ShowCode();
        }


        private void InitData()
        {
            if (cbReturnTypes.Items.Count > 0)
            {
                cbReturnTypes.SelectedIndex = 0;
            }

            if (_selectedProperties == null || _selectedProperties.Columns.Count <= 0)
            {
                lstProperties.Enabled = false;
                return;
            }

            lstProperties.Enabled = true;
            lstProperties.Items.Clear();

            foreach (DataColumn column in _selectedProperties.Columns)
            {
                lstProperties.Items.Add(column.ColumnName);
            }
        }

        private void ShowCode()
        {
            if (lstProperties.SelectedItems.Count <= 0)
            {
                txtMethodBody.Text = "";
                return;
            }

            txtMethodName.Text = txtMethodName.Text.Trim();

            StringBuilder propertiesList=new StringBuilder();
            StringBuilder methodBody = new StringBuilder();

            GetMethodCode(ref propertiesList, ref methodBody);

            string className = _codeProvider.MapClassName(_selectedProperties.TableName);

            string returnType = "DataTable";
            if (cbReturnTypes.SelectedItem.ToString().Equals("List"))
            {
                returnType = "List<" + className+">";
            }

            string code = "public " + returnType + " ";
            code += string.IsNullOrEmpty(txtMethodName.Text) ? "MyMethod" : txtMethodName.Text;
            code += "(" + propertiesList.ToString() + ")\n";
            code += "{\n";
            code +=methodBody.ToString();
            code += "}\n";

            txtMethodBody.Text = code;
        }

        private string GetMethodCode(ref StringBuilder propertiesList,ref StringBuilder methodBody)
        {
            if (_codeProvider == null)
            {
                return "";
            }

            if (lstProperties.SelectedItems.Count <= 0)
            {
                return "";
            }

            string className = _codeProvider.MapClassName(_selectedProperties.TableName);

            int i = 0;
            foreach (object item in lstProperties.SelectedItems)
            {
                DataColumn column=_selectedProperties.Columns[item.ToString()];

                propertiesList.Append(column.DataType.Name.ToString() + " " +
                    _codeProvider.ToFieldName(column.ColumnName).Remove(0, 1));
                propertiesList.Append(",");

                if (i == 0)
                {
                    methodBody.Append("Expression exp = Expression.Equal(\"" +
                        column.ColumnName + "\"," + _codeProvider.ToFieldName(column.ColumnName).Remove(0, 1) + ");\n");
                }
                else
                {
                    if (rbtnAnd.Checked)
                    {
                        methodBody.Append("exp &= ");
                    }
                    else
                    {
                        methodBody.Append("exp |= ");
                    }
                    methodBody.Append("Expression.Equal(\"" +
                        column.ColumnName + "\"," + _codeProvider.ToFieldName(column.ColumnName).Remove(0, 1) + ");\n");
                }

                i++;
            }

            
            if (cbReturnTypes.SelectedItem.ToString().Equals("List"))
            {
                methodBody.Append("return "+className+".Find(exp);\n");
            }
            else if (!cbReturnTypes.SelectedItem.ToString().Equals("DataTable"))
            {
                methodBody.Append("return "+className+".Query(exp);\n");
            }

            if (propertiesList.Length > 0)
            {
                propertiesList.Remove(propertiesList.Length - 1, 1);
            }

            return propertiesList.ToString();
        }

       

        


    }
}
